package com.demo.common.job;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.demo.webmagic.pageProcessor.PageProcessorAnxiaomo;
import com.demo.webmagic.pageProcessor.PageProcessorGoubanjia;
import com.demo.webmagic.pageProcessor.PageProcessorHaodaili;
import com.demo.webmagic.pageProcessor.PageProcessorKuaidaili;
import com.demo.webmagic.pipeline.PipelineProxyIp;

import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.downloader.selenium.SeleniumDownloader;

public class JobProxyIp implements Job {

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		// 快代理
		String[] kuaidailiUrlList = new String[3];
		for (int i = 1; i <= 3; i++) {
			kuaidailiUrlList[i - 1] = "http://www.kuaidaili.com/free/inha/" + i;
		}
		Spider.create(new PageProcessorKuaidaili()).addUrl(kuaidailiUrlList).addPipeline(new PipelineProxyIp()).start();
		// 好代理
		String[] haodailiUrlList = new String[10];
		for (int i = 1; i <= 10; i++) {
			haodailiUrlList[i - 1] = "http://www.haodailiip.com/guonei/" + i;
		}
		Spider.create(new PageProcessorHaodaili()).addUrl(haodailiUrlList).addPipeline(new PipelineProxyIp()).start();

		// 安小莫
		Spider.create(new PageProcessorAnxiaomo()).addPipeline(new PipelineProxyIp())
				.addUrl("http://www.66ip.cn/nmtq.php?getnum=800&isp=0&anonymoustype=4&start=&ports=&export=&ipaddress=&area=1&proxytype=0&api=66ip")
				.start();

		// 狗搬家
		try {
			Spider.create(new PageProcessorGoubanjia()).addPipeline(new PipelineProxyIp())
					.setDownloader(new SeleniumDownloader(
							URLDecoder.decode(PageProcessorAnxiaomo.class.getClass().getResource("/").getPath(),
									"utf-8") + "chromedriver.exe"))
					.start();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

}
